
#ifndef  VARINTUTIL_H
#define  VARINTUTIL_H

#include "common.h"

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;

#define SQLITE_MAX_U32  ((((u64)1)<<32)-1)

/*
 * ** The common case is for a varint to be a single byte.  They following
 * ** macros handle the common case without a procedure call, but then call
 * ** the procedure for larger varints.
 * */
#define getVarint32(A,B)  \
  (u8)((*(A)<(u8)0x80) ? ((B)=(u32)*(A)),1 : sqlite3GetVarint32((A),(u32 *)&(B)))
#define putVarint32(A,B)  \
    (u8)(((u32)(B)<(u32)0x80) ? (*(A)=(unsigned char)(B)),1 : sqlite3PutVarint((A),(B)))

#define getVarint    sqlite3GetVarint
#define putVarint    sqlite3PutVarint

// v -> buffer
int sqlite3PutVarint(unsigned char *p, u64 v);
// buffer -> (len, v_64)
u8 sqlite3GetVarint(const unsigned char *p, u64 *v);

// buffer -> (len, v_32)
u8 sqlite3GetVarint32(const unsigned char *p, u32 *v);


int sqlite3VarintLen(u64 v);


void VarIntUnitTest();

#endif   /* VARINTUTIL_H */
